/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proyecto;


import com.proyecto.datos.EntityMateria;
import com.proyecto.datos.EntityPlan;
import com.proyecto.modelado.Materia;
import com.proyecto.modelado.Plan;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author mario
 */
public class frmModificarMateria extends javax.swing.JInternalFrame {

    Plan pla;
    Materia mate;
    ArrayList arryPla;
    int id_plan;
    
    
    public void PopularTabla() {

        //POPULANDO TABLE 10

        DefaultTableModel tabla1;
        Object[] fila = new Object[4];
        try {
            String[] columName = {"ID", "Codigo","Materia","Plan"};
            tabla1 = new DefaultTableModel(null, columName);
            //populamos el arraylist de la tabla
            ArrayList arry_10 = new ArrayList();
            mate = new Materia();
            EntityMateria entMate = new EntityMateria();

            arry_10 = mate.MostrarMaterias();
            if (arry_10 != null) {
                for (int i = 0; i < arry_10.size(); i++) {
                    entMate = (EntityMateria) arry_10.get(i);
                    fila[0] = entMate.getId_materia();
                    fila[1] = entMate.getCodigo_asignatura();
                    fila[2] = entMate.getNombre();
                    fila[3] = entMate.getEntNombre_plan();
                    
                    

                    tabla1.addRow(fila);
                }




                this.jtbleCatedratico.setModel(tabla1);
            } else {
                JOptionPane.showMessageDialog(rootPane, "No se recuperaron datos de la base de datos", "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.toString());
        }
    }
    
    
    public void cargarComboBox(){
        try{
            
         arryPla = new ArrayList();
            pla = new Plan();
            arryPla = pla.MostrarPlan();
            JList list4 = new JList();
            DefaultListModel listModel4 = new DefaultListModel();
            listModel4.addElement("Seleccione");
            if (arryPla != null) {
                for (int i = 0; i < arryPla.size(); i++) {
                    EntityPlan entPla = (EntityPlan) arryPla.get(i);
                    listModel4.addElement(entPla.getNombre_plan());
                }
            } else {
                JOptionPane.showMessageDialog(rootPane, "No se recuperaron datos de la tabla Materia", "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);

            }
            list4.setModel(listModel4);
            this.jcmbPlan.setModel(new DefaultComboBoxModel(listModel4.toArray()));

        } catch (Exception ex) {
            System.out.println(ex.toString());
        }

    
    }
     
     
     public void BloquearControles(){
         this.jtxtCodAsig.setEnabled(false);
         this.jtxtID.setEnabled(false);
         this.jtxtMateria.setEnabled(false);
         
     }
     
     public void Habilitarcontroles(){
         this.jtxtCodAsig.setEnabled(true);
         this.jtxtID.setEnabled(true);
         this.jtxtMateria.setEnabled(true);
         
     }
    
    
    public frmModificarMateria() {
        initComponents();
        
        BloquearControles();
        
        PopularTabla();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbtnEliminar = new javax.swing.JButton();
        jbtnActualizar = new javax.swing.JButton();
        jbtnCargar = new javax.swing.JButton();
        jbtnCancelar = new javax.swing.JButton();
        jtxtCodAsig = new javax.swing.JTextField();
        jtxtID = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbleCatedratico = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtxtMateria = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jcmbPlan = new javax.swing.JComboBox();

        jbtnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/images (2).jpg"))); // NOI18N
        jbtnEliminar.setText("Eliminar");
        jbtnEliminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnEliminarMouseClicked(evt);
            }
        });

        jbtnActualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/editar.png"))); // NOI18N
        jbtnActualizar.setText("Actualizar");
        jbtnActualizar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnActualizarMouseClicked(evt);
            }
        });

        jbtnCargar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/cargar.png"))); // NOI18N
        jbtnCargar.setText("Cargar");
        jbtnCargar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnCargarMouseClicked(evt);
            }
        });
        jbtnCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCargarActionPerformed(evt);
            }
        });

        jbtnCancelar.setText("Cancelar");
        jbtnCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnCancelarMouseClicked(evt);
            }
        });
        jbtnCancelar.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jbtnCancelarItemStateChanged(evt);
            }
        });

        jtxtCodAsig.setText("Codigo");

        jtxtID.setText("ID");

        jtbleCatedratico.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jtbleCatedratico.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jScrollPane1.setViewportView(jtbleCatedratico);

        jLabel1.setText("ID:");

        jLabel2.setText("Codigo:");

        jLabel3.setText("Materia:");

        jtxtMateria.setText("Materia");

        jLabel4.setText("Plan:");

        jcmbPlan.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcmbPlanItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jbtnCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jbtnCargar, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(52, 52, 52)
                                .addComponent(jbtnActualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(53, 53, 53)
                                .addComponent(jbtnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(23, 23, 23)
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(jtxtID, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(30, 30, 30)
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(jtxtCodAsig, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(39, 39, 39)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtxtMateria, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                                .addComponent(jLabel4)
                                .addGap(25, 25, 25)))
                        .addComponent(jcmbPlan, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 27, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtCodAsig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jtxtMateria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jcmbPlan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnActualizar)
                    .addComponent(jbtnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnCargar))
                .addGap(33, 33, 33)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtnCancelar)
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnEliminarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnEliminarMouseClicked
        try{
            int id_materia=Integer.parseInt(this.jtxtID.getText());

            mate = new Materia();
            String msj=mate.eliminarMateria(id_materia);

            JOptionPane.showMessageDialog(rootPane, msj,"Confirmacion",JOptionPane.INFORMATION_MESSAGE);

            PopularTabla();

        }catch(Exception ex){
            JOptionPane.showMessageDialog(rootPane, ex.toString(),"Error",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbtnEliminarMouseClicked

    private void jbtnActualizarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnActualizarMouseClicked

        try{
            int id_materia=Integer.parseInt(this.jtxtID.getText());
            String cod_asig=this.jtxtCodAsig.getText();
            String nombre_materia=this.jtxtMateria.getText();
            int id_plan=this.id_plan;
            

            mate = new Materia();
            String msj=mate.modificarMateria(id_materia, cod_asig, nombre_materia, id_plan);

            JOptionPane.showMessageDialog(rootPane, msj,"Confirmacion",JOptionPane.INFORMATION_MESSAGE);

            PopularTabla();

        }catch(Exception ex){
            JOptionPane.showMessageDialog(rootPane, ex.toString(),"Error",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbtnActualizarMouseClicked

    private void jbtnCargarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnCargarMouseClicked
        Habilitarcontroles();
        cargarComboBox();

        int fila=this.jtbleCatedratico.getSelectedRow();

        this.jtxtID.setText(String.valueOf(this.jtbleCatedratico.getValueAt(fila, 0)));
        this.jtxtCodAsig.setText(String.valueOf(this.jtbleCatedratico.getValueAt(fila, 1)));
        this.jtxtMateria.setText(String.valueOf(this.jtbleCatedratico.getValueAt(fila, 2)));
        

    }//GEN-LAST:event_jbtnCargarMouseClicked

    private void jbtnCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCargarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jbtnCargarActionPerformed

    private void jbtnCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnCancelarMouseClicked

        this.dispose();
    }//GEN-LAST:event_jbtnCancelarMouseClicked

    private void jbtnCancelarItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jbtnCancelarItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_jbtnCancelarItemStateChanged

    private void jcmbPlanItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcmbPlanItemStateChanged
        ArrayList arryPlan = new ArrayList();
        
        pla = new Plan();
        arryPlan=pla.MostrarPlan();
        EntityPlan entPla = new EntityPlan();
        
        for (int i = 0; i < arryPlan.size(); i++) {
            entPla = (EntityPlan) arryPlan.get(i);
            if (this.jcmbPlan.getSelectedItem().equals(entPla.getNombre_plan())) {
                this.id_plan = entPla.getId_plan();
            }
        }
    }//GEN-LAST:event_jcmbPlanItemStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnActualizar;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JButton jbtnCargar;
    private javax.swing.JButton jbtnEliminar;
    private javax.swing.JComboBox jcmbPlan;
    private javax.swing.JTable jtbleCatedratico;
    private javax.swing.JTextField jtxtCodAsig;
    private javax.swing.JTextField jtxtID;
    private javax.swing.JTextField jtxtMateria;
    // End of variables declaration//GEN-END:variables
}
